import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.*;


public class socketChat {
	private String username;
	public String hostName;
	public String message;
	public int portNumber = 8888;
	Socket connectionSocket;
	ServerSocket serverSocket;
	ObjectOutputStream out;
	ObjectInputStream in;
	
	
	public socketChat(){
	}
	
	public void run(){
		try{
			
			serverSocket = new ServerSocket(8888);
			Socket connectionSocket = serverSocket.accept();
			System.out.println("is socket open");
			
			out = new ObjectOutputStream(connectionSocket.getOutputStream());
			out.flush();
			in = new ObjectInputStream(connectionSocket.getInputStream());
			connectionSocket.close();
			sendMessage("Connection successfull");
			System.out.print("good?");
			
			do{
				try{
					message = (String)in.readObject();
					System.out.println("client >" + message);
					if(message.equals("bye")) sendMessage("bye");
				}
				catch(ClassNotFoundException classNot){
					System.err.println("data received in unknown format");
				}
			}while(!message.equals("bye"));
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		finally{
			try{
				in.close();
				out.close();
				serverSocket.close();
			}
			catch(IOException ioException){
				ioException.printStackTrace();
			}
		}
	}
	
	public void sendMessage(String message)
	{
		try{
			out.writeObject(message);
			out.flush();
			System.out.println("Server>" + message);
		}
		catch(IOException e){
			e.printStackTrace();
		}
	}
	
	public static void main(String args[]){
		System.out.println("is it working");
		socketChat server = new socketChat();
		while(true){
			server.run();
		}
		
		
	}
}
